<?php

namespace app\api\model;

use think\Model;
use traits\model\SoftDelete;

class Orders extends Model
{
    use SoftDelete;
    protected $autoWriteTimestamp = 'int';
    protected $deleteTime = 'deletetime';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $hidden = ['deletetime', 'updatetime'];

    public function getCreateTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }

    public static function createOrder($order)
    {
        return self::create([
            'order_no' => $order['order_no'],
            'user_id' => $order['user_id'],
            'design_id' => $order['design_id'],
            'design_service_id' => $order['design_service_id'],
            'user_coupon_id' => $order['user_coupon_id'] ?? 0,
            'phone' => $order['phone'] ?? '',
            'time' => $order['time'] ?? '',
            'service_name' => $order['service_name'],
            'total_amount' => $order['total_money'],
            'vip_amount' => $order['vip_amount'],
            'discount_amount' => $order['discount_amount'],
            'status' => '0',
            'orderdata' => $order['orderdata']
        ]);
    }

    public function design()
    {
        return $this->hasOne(Designs::class, 'id', 'design_id');
    }

    public function refund()
    {
        return $this->hasOne(Refunds::class, 'order_id', 'id');
    }

    public function user()
    {
        return $this->hasOne(Users::class, 'id', 'user_id');
    }

    public function designService()
    {
        return $this->hasOne(DesignServices::class, 'id', 'design_service_id');
    }

    public static function getUserOrders($user_id, $type = 0, $length = 15)
    {
        $status = self::status($type);
        $orders = self::with('design')->where('user_id', $user_id)->whereIn('status', $status)->order('createtime desc')->paginate($length);
        if ($orders->items()) {
            foreach ($orders->items() as $val) {
                $val['qrcode'] = $val['status'] == 1 ? Qrcode::createQrcode($val['id']) : '';
            }
        }
        return $orders;
    }

    public static function status($type)
    {
        switch ($type) {
            //待消费
            case 1:
                $status = [1];
                break;
            //服务中
            case 2:
                $status = [2];
                break;
            //已完成
            case 3:
                $status = [3];
                break;
            //取消
            case 4:
                $status = [4, 5];
                break;
            default:
                $status = [1, 2, 3, 4, 5];
                break;
        }
        return $status;
    }

    public static function getOrderByOrderNo($order_no)
    {
        return self::where('order_no', $order_no)->find();
    }

    /**
     * 造型师预约列表
     */
    public static function getOrdersByDesign($design_id, $date = '', $status, $length = 15)
    {
        $where = [
            'status' => ['=', $status]
        ];
        if ($date) {
            $start_time = strtotime($date);
            $end_time = $start_time + 86399;
            $orders = self::with('user')->where('design_id', $design_id)->where($where)->whereTime('time', 'between', [$start_time, $end_time])->paginate($length);
        } else {
            $orders = self::with('user')->where('design_id', $design_id)->where($where)->paginate($length);
        }
        return $orders;
    }

    /**
     * 造型师某天的预约情况
     */
    public static function getOrderByTime($design_id, $date)
    {
        $start_time = strtotime($date);
        $end_time = $start_time + 24 * 3600 - 1;
        $order = self::where('design_id',$design_id)->whereIn('status', [1, 2])->whereTime('time', 'between', [$start_time, $end_time])->column('time');
        $order = array_unique($order);
        array_walk($order, function (&$val) {
            $val = date('H:i', strtotime($val));
        });
        return array_values($order);
    }
}
